//
//  RegisterView.m
//  Ev_Master
//
//  Created by Evan on 2019/6/24.
//  Copyright © 2019 Evan. All rights reserved.
//

#import "RegisterView.h"
#import <Masonry.h>
#import "CommonData.h"
#import "taskUserService.h"
#import "DKProgressHUD.h"


@implementation RegisterView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        [self labelPhone];
        [self txtPhone];
        [self viewline];
        
        [self labelName];
        [self txtName];
        [self viewline2];
        
        [self labelInv];
        [self txtInv];
        [self viewline3];
        
        [self labelCode];
        [self btnSendCode];
        [self txtCode];
        [self viewline4];
        
        [self labelPwd];
        [self txtPwd];
        [self viewline5];
        
        [self btnRegister];
        [self btnNote];
    }
    return self;
}

///手机号

-(UILabel*)labelPhone{
    if (!_labelPhone) {
        _labelPhone = [[UILabel alloc]init];
        [self addSubview:_labelPhone];
        [_labelPhone mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(20);
            make.top.mas_equalTo(self).offset(10);
            make.right.mas_equalTo(self).offset(-20);
            make.height.offset(20);
        }];
        _labelPhone.text = @"请输手机号";
        _labelPhone.font = [UIFont systemFontOfSize:14];
        _labelPhone.textColor = [UIColor whiteColor];
    }
    return _labelPhone;
}

-(UITextField*)txtPhone{
    if (!_txtPhone) {
        _txtPhone = [[UITextField alloc]init];
        [self addSubview:_txtPhone];
        [_txtPhone mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.labelPhone.mas_bottom);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.offset(30);
        }];
        _txtPhone.font = [UIFont systemFontOfSize:17];
        _txtPhone.textColor = [UIColor whiteColor];
    }
    return _txtPhone;
}
-(UIView*)viewline{
    if (!_viewline) {
        _viewline = [[UIView alloc]init];
        [self addSubview:_viewline];
        [_viewline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.txtPhone.mas_bottom);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.offset(1);
        }];
        _viewline.backgroundColor = COLOR_Line;
    }
    return _viewline;
}


///用户名

-(UILabel*)labelName{
    if (!_labelName) {
        _labelName = [[UILabel alloc]init];
        [self addSubview:_labelName];
        [_labelName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.viewline.mas_bottom).offset(10);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.mas_equalTo(self.labelPhone.mas_height);
        }];
        _labelName.text = @"用户名";
        _labelName.font = [UIFont systemFontOfSize:14];
        _labelName.textColor = [UIColor whiteColor];
    }
    return _labelName;
}

-(UITextField*)txtName{
    if (!_txtName) {
        _txtName = [[UITextField alloc]init];
        [self addSubview:_txtName];
        [_txtName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.labelName.mas_bottom);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.mas_equalTo(self.txtPhone.mas_height);
        }];
        _txtName.font = [UIFont systemFontOfSize:17];
        _txtName.textColor = [UIColor whiteColor];
    }
    return _txtName;
}
-(UIView*)viewline2{
    if (!_viewline2) {
        _viewline2 = [[UIView alloc]init];
        [self addSubview:_viewline2];
        [_viewline2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.txtName.mas_bottom);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.offset(1);
        }];
        _viewline2.backgroundColor = COLOR_Line;
    }
    return _viewline2;
}


///推荐人

-(UILabel*)labelInv{
    if (!_labelInv) {
        _labelInv = [[UILabel alloc]init];
        [self addSubview:_labelInv];
        [_labelInv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.viewline2.mas_bottom).offset(10);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.mas_equalTo(self.labelPhone.mas_height);
        }];
        _labelInv.text = @"推荐人";
        _labelInv.font = [UIFont systemFontOfSize:14];
        _labelInv.textColor = [UIColor whiteColor];
    }
    return _labelInv;
}

-(UITextField*)txtInv{
    if (!_txtInv) {
        _txtInv = [[UITextField alloc]init];
        [self addSubview:_txtInv];
        [_txtInv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.labelInv.mas_bottom);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.mas_equalTo(self.txtPhone.mas_height);
        }];
        _txtInv.font = [UIFont systemFontOfSize:17];
        _txtInv.textColor = [UIColor whiteColor];
    }
    return _txtInv;
}
-(UIView*)viewline3{
    if (!_viewline3) {
        _viewline3 = [[UIView alloc]init];
        [self addSubview:_viewline3];
        [_viewline3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.txtInv.mas_bottom);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.offset(1);
        }];
        _viewline3.backgroundColor = COLOR_Line;
    }
    return _viewline3;
}


///验证码

-(UILabel*)labelCode{
    if (!_labelCode) {
        _labelCode = [[UILabel alloc]init];
        [self addSubview:_labelCode];
        [_labelCode mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.viewline3.mas_bottom).offset(10);
            make.width.offset(150);
            make.height.mas_equalTo(self.labelPhone.mas_height);
        }];
        _labelCode.text = @"请输入验证码";
        _labelCode.font = [UIFont systemFontOfSize:14];
        _labelCode.textColor = [UIColor whiteColor];
    }
    return _labelCode;
}

-(UIButton*)btnSendCode{
    if (!_btnSendCode) {
        _btnSendCode = [[UIButton alloc]init];
        [self addSubview:_btnSendCode];
        [_btnSendCode mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(120);
            make.top.mas_equalTo(self.viewline3.mas_bottom).offset(10);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.mas_equalTo(self.txtPhone.mas_height);
        }];
        [_btnSendCode setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_btnSendCode.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [_btnSendCode.titleLabel setTextColor:[UIColor whiteColor]];
        [_btnSendCode addTarget:self action:@selector(btnSendCodeClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnSendCode;
}

-(UITextField*)txtCode{
    if (!_txtCode) {
        _txtCode = [[UITextField alloc]init];
        [self addSubview:_txtCode];
        [_txtCode mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.labelCode.mas_bottom);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.mas_equalTo(self.txtPhone.mas_height);
        }];
        _txtCode.font = [UIFont systemFontOfSize:17];
        _txtCode.textColor = [UIColor whiteColor];
    }
    return _txtCode;
}
-(UIView*)viewline4{
    if (!_viewline4) {
        _viewline4 = [[UIView alloc]init];
        [self addSubview:_viewline4];
        [_viewline4 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.txtCode.mas_bottom);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.offset(1);
        }];
        _viewline4.backgroundColor = COLOR_Line;
    }
    return _viewline4;
}


///密码

-(UILabel*)labelPwd{
    if (!_labelPwd) {
        _labelPwd = [[UILabel alloc]init];
        [self addSubview:_labelPwd];
        [_labelPwd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.viewline4.mas_bottom).offset(10);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.mas_equalTo(self.labelPhone.mas_height);
        }];
        _labelPwd.text = @"请输入密码";
        _labelPwd.font = [UIFont systemFontOfSize:14];
        _labelPwd.textColor = [UIColor whiteColor];
    }
    return _labelPwd;
}

-(UITextField*)txtPwd{
    if (!_txtPwd) {
        _txtPwd = [[UITextField alloc]init];
        [self addSubview:_txtPwd];
        [_txtPwd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.labelPwd.mas_bottom);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.mas_equalTo(self.txtPhone.mas_height);
        }];
        _txtPwd.font = [UIFont systemFontOfSize:17];
        _txtPwd.textColor = [UIColor whiteColor];
        _txtPwd.secureTextEntry = YES;
    }
    return _txtPwd;
}
-(UIView*)viewline5{
    if (!_viewline5) {
        _viewline5 = [[UIView alloc]init];
        [self addSubview:_viewline5];
        [_viewline5 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.txtPwd.mas_bottom);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.offset(1);
        }];
        _viewline5.backgroundColor = COLOR_Line;
    }
    return _viewline5;
}


///注册按钮
-(UIButton*)btnRegister{
    if (!_btnRegister) {
        _btnRegister = [[UIButton alloc]init];
        [self addSubview:_btnRegister];
        [_btnRegister mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.viewline5.mas_bottom).offset(40);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.offset(55);
        }];
        [_btnRegister.titleLabel setTextColor:[UIColor whiteColor]];
        [_btnRegister setTitle:@"注册" forState:UIControlStateNormal];
        [_btnRegister setBackgroundImage:[UIImage imageNamed:@"BGbtnLogin"] forState:UIControlStateNormal];
        [_btnRegister addTarget:self action:@selector(btnRegisterClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnRegister;
}


-(UIButton*)btnNote{
    if (!_btnNote) {
        _btnNote = [[UIButton alloc]init];
        [self addSubview:_btnNote];
        [_btnNote mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.labelPhone.mas_left);
            make.top.mas_equalTo(self.btnRegister.mas_bottom);
            make.right.mas_equalTo(self.labelPhone.mas_right);
            make.height.offset(55);
        }];
        [_btnNote.titleLabel setTextColor:[UIColor whiteColor]];
        _btnNote.titleLabel.font = [UIFont systemFontOfSize:13];
        [_btnNote setTitle:@"注册即表示同意《用户协议》" forState:UIControlStateNormal];
    }
    return _btnNote;
}


#pragma mark - 注册按钮---开始注册

- (void)btnRegisterClick:(UIButton*)sender{
    if ([self.txtPhone.text  isEqualToString:@""] || [self.txtInv.text  isEqualToString:@""] ||
        [self.txtPwd.text  isEqualToString:@""] || [self.txtCode.text  isEqualToString:@""] ||
        [self.txtName.text  isEqualToString:@""] ) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的注册信息"];
        return;
    }
    [DKProgressHUD showLoading];
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:self.txtPhone.text forKey:@"mobile"];
    [dict setValue:self.txtName.text forKey:@"username"];
    [dict setValue:self.txtPwd.text forKey:@"password"];
    [dict setValue:self.txtCode.text forKey:@"verifyCode"];
    [dict setValue:self.txtInv.text forKey:@"RefereeUser"];
    [dict setValue:self.txtInv.text forKey:@"ParentUser"];
    [dict setValue:@"true" forKey:@"agree"];
    
    [taskUserService userRegister:dict success:^(modelUserData * _Nonnull response, BOOL success) {
        [DKProgressHUD dismiss];
        //注册成功
        [DKProgressHUD showSuccessWithStatus:@"注册成功"];
        [self clearRegisterText];
        self.blockRegister();
    } failture:^(NSError * _Nonnull error, NSString * _Nonnull strDescription) {
        //注册失败
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

///清空注册信息
-(void)clearRegisterText{
    self.txtPhone.text = @"";
    self.txtName.text = @"";
    self.txtPwd.text = @"";
    self.txtCode.text = @"";
    self.txtInv.text = @"";
}


#pragma mark - 发送验证码

-(void)btnSendCodeClick:(UIButton*)sender{
    
    if ([self.txtPhone.text  isEqualToString:@""]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    
    [DKProgressHUD showLoading];
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:self.txtPhone.text forKey:@"mobile"];
    [dict setValue:@"用户注册" forKey:@"usage"];
    [taskUserService sendSMS:dict success:^(BaseModel * _Nonnull response, BOOL success) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showSuccessWithStatus:response.message];
        
        [self startCount];
        
    } failture:^(NSError * _Nonnull error, NSString * _Nonnull strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}




#pragma mark - 倒计时
- (void)startCount
{
    /**
     *  添加定时器
     */
    self.currentCountDown = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    [self.timer fire];
}

- (void)countDown{
    
    if (self.currentCountDown >0) {
        //设置界面的按钮显示 根据自己需求设置
        [self.btnSendCode setTitle:[NSString stringWithFormat:@"(%ld)重新获取",(long)self.currentCountDown] forState:UIControlStateNormal];
        self.btnSendCode.enabled = NO;
        self.currentCountDown -= 1;
    }else{
        [self removeTimer];
        self.btnSendCode.enabled = YES;
    }
    
}

/**
 *  移除定时器
 */
- (void)removeTimer
{
    self.currentCountDown = 0;
    [self setCaptchaEnable:YES];
    [self.timer invalidate];
//    self.timer = nil;
}


//因为iOS 7下 按钮 enabled= NO, 不能设置文字
#pragma mark - 设置按钮状态
- (void)setCaptchaEnable:(BOOL)enabled{
    //可以按
    if (enabled) {
        self.btnSendCode.userInteractionEnabled = YES;
        [self.btnSendCode setTitle:@"获取验证码" forState:UIControlStateNormal];
        
    }else{
        self.btnSendCode.userInteractionEnabled = NO;
    }
}


@end
